<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

//require_once 'Zend_Acl_Role.php';

// Code here is run before admin controllers
class Admin_Controller extends MY_Controller
{
	public $logger = null;
	public $transLogger = null;

	function Admin_Controller()
	{
		parent::MY_Controller();

		//$this->_checkAuthenticate();
		$this->load->model('modules_m');
		$this->load->library('zacl');
		// Load the Language files ready for output
	    $this->lang->load('admin');

	    $allow_access = FALSE;

	    // These pages get past permission checks
	    $ignored_pages = array('admin/login', 'admin/logout', 'admin/insufficient_permission');

		$resource = $this->module;
		if ($this->controller)
			$resource .= '/'.$this->controller;
		$privilege = $this->method;

		if($resource == '_error_404' || $resource == 'admin/insufficient_permission'){
    		return;
    	}
        // Check if the current page is to be ignored
	    $current_page = $this->uri->segment(1, '') . '/' . $this->uri->segment(2, '');
	    $is_ignored_page = in_array($current_page, $ignored_pages);

	   	//Kiem tra quyen truy cap trong truong hop khong thuoc ignorepage va userid != 1
	   	$zendAclRole = isset($this->data->user->role) ? new Zend_Acl_Role($this->data->user->role) : new Zend_Acl_Role('guest');
		if ($this->userId() != 1 && !$is_ignored_page && !$this->zacl->check_acl($zendAclRole, $resource, $privilege))
        {
        	if ($this->session->userdata('user_id'))
            	redirect('admin/insufficient_permission');
            else {
            	redirect('admin/login?return_url='.base64_encode($this->uri->full_uri_address()));
            }
        }

	    // Login: If not logged in and its not an ignored page, force login
	    if( ! $this->data->user && ! $is_ignored_page)
	    {
	    	redirect('admin/login?return_url='.base64_encode($this->uri->full_uri_address()));
	    }
	    // Logged in or ignored page
	    else
	    {
	    	$allow_access = TRUE;
	    }

	    /*// We are looking at the index page. Show it if they have ANY admin access at all
	    if( in_array($current_page, array('admin/', 'admin/index')) && $this->permissions_m->hasAdminAccess($this->data->user->role) )
	    {
	    	$allow_access = TRUE;
	    }
	    // Check Perms: Not an admin and this is not a page to ignore
	    elseif( ! $is_admin && ! $is_ignored_page )
	    {
		  	// Check if the current user can view that page
		    $location = array( 'module'=>$this->module, 'controller'=>$this->controller, 'method'=>$this->method );
		    $allow_access = $this->permissions_m->checkRuleByRole( $this->data->user->role, $location );
	    }

	    // Show error and exit if the user does not have sufficient permissions
	    if( ! $allow_access )
	    {
		  	show_error($this->lang->line('cp_access_denied'));
		    exit;
	    }*/

	    if($current_page != 'admin/login')
	    {
	  		$this->data->admin_modules = $this->cache->model('modules_m', 'getModules', array(
	    		array(
					'is_backend_sidebar'=>true,
					'role' => @$this->data->user->role,
	    			'lang' => CURRENT_LANGUAGE
				) // This function does NOT need role OR language, that is to give it a unique md5 hash
	    	), $this->cache->cache_timeout);
		}
	    //$this->data->toolbar = $this->modules_m->getModuleToolbar($this->module);

	    $this->layout->wrapper('ckv_admin/layout');

	    //$this->output->enable_profiler(TRUE);
	}

	public function userId(){
		if ($this->session->userdata('user_id')){
			return $this->session->userdata('user_id');
		}else{
			return -1;
		}
	}

	public function userInfo(){
		if ($this->userId() > -1){
			return $this->users_m->getUser(array('id'=>$this->userId()));
		}else{
			return false;
		}
	}

	function setAccountLogger($content){
		$this->load->model('user_logger_m');
		$user_id = $this->session->userdata('user_id');
		$this->user_logger_m->insert(array('user_id' => $user_id,'content'=>$content,'ip'=>$this->input->ip_address()));
	}
}
